package com.hy.study.mapper;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * 日志清理
 *
 * @author huyong
 * @date 2024 年 02 月 06 日 15:49: 38
 */
public class LogMapper extends Mapper<LongWritable, Text, Text, NullWritable> {

    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, NullWritable>.Context context) throws IOException, InterruptedException {
        String line = value.toString();
        // 过滤脏数据
        boolean result = this.parseLog(line, context);
        if (!result) {
            return;
        }
        // 数据解析成功
        context.write(value, NullWritable.get());
    }

    private boolean parseLog(String line, Mapper<LongWritable, Text, Text, NullWritable>.Context context) {
        try {
            if (StringUtils.isBlank(line)) {
                return false;
            }
            String[] split = line.split(" ");
            if (split.length > 11) {// 判断记录的内容是否大于11个
                context.getCounter("map", "true").increment(1L);// 添加一个计数器
                return true;
            } else {
                context.getCounter("map", "false").increment(1L);// 添加一个计数器
                return false;
            }

        } catch (Exception e) {
            return false;
        }
    }
}
